package com.kymatrix.mpo.plus.bpm.server.controller.definition;

import com.kymatrix.mpo.plus.bpm.server.model.entity.definition.BpmProcessExpressionEntity;
import com.kymatrix.mpo.plus.bpm.server.model.form.expression.PageQueryBpmProcessExpressionPageReq;
import com.kymatrix.mpo.plus.bpm.server.model.form.expression.QueryBpmProcessExpressionResp;
import com.kymatrix.mpo.plus.bpm.server.model.form.expression.SaveBpmProcessExpressionReq;
import com.kymatrix.mpo.plus.bpm.server.model.form.expression.UpdateBpmProcessExpressionReq;
import com.kymatrix.mpo.plus.bpm.server.service.BpmProcessExpressionService;
import com.kymatrix.mpo.plus.common.business.common.PageResp;
import com.kymatrix.mpo.plus.common.business.common.ResultResp;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import javax.annotation.Resource;
import javax.validation.Valid;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Api(tags = "BPM 流程表达式相关请求处理类")
@RestController
@Validated
public class BpmProcessExpressionController {

    @Resource
    private BpmProcessExpressionService processExpressionService;

    @PostMapping("/bpm/process-expression/create")
    @ApiOperation(value = "创建流程表达式")
    public ResultResp<String> createProcessExpression(@Valid @RequestBody SaveBpmProcessExpressionReq req) {
        return ResultResp.ok(processExpressionService.createProcessExpression(req));
    }

    @PutMapping("/bpm/process-expression/update")
    @ApiOperation(value = "更新流程表达式")
    public ResultResp<Boolean> updateProcessExpression(@Valid @RequestBody UpdateBpmProcessExpressionReq req) {
        processExpressionService.updateProcessExpression(req);
        return ResultResp.ok(true);
    }

    @DeleteMapping("/bpm/process-expression/delete")
    @ApiOperation(value = "删除流程表达式")
    public ResultResp<Boolean> deleteProcessExpression(@RequestParam("id") String id) {
        processExpressionService.deleteProcessExpression(id);
        return ResultResp.ok(true);
    }

    @GetMapping("/bpm/process-expression/get")
    @ApiOperation(value = "获得流程表达式")
    public ResultResp<QueryBpmProcessExpressionResp> getProcessExpression(@RequestParam("id") String id) {
        BpmProcessExpressionEntity processExpression = processExpressionService.getProcessExpression(id);
        if (processExpression == null) {
            return ResultResp.ok();
        }
        QueryBpmProcessExpressionResp resp = new QueryBpmProcessExpressionResp();
        resp.setId(processExpression.getId());
        resp.setName(processExpression.getName());
        resp.setStatus(processExpression.getStatus());
        resp.setExpression(processExpression.getExpression());
        resp.setCreateTime(processExpression.getCreateTime());
        return ResultResp.ok(resp);
    }

    @GetMapping("/bpm/process-expression/page")
    @ApiOperation(value = "获得流程表达式分页")
    public ResultResp<PageResp<QueryBpmProcessExpressionResp>> getProcessExpressionPage(
            @Valid PageQueryBpmProcessExpressionPageReq req) {
        PageResp<BpmProcessExpressionEntity> pageResult = processExpressionService.getProcessExpressionPage(req);
        PageResp<QueryBpmProcessExpressionResp> resp = new PageResp<>();
        resp.setTotal(pageResult.getTotal());
        for (BpmProcessExpressionEntity entity : pageResult.getList()) {
            resp.getList().add(QueryBpmProcessExpressionResp.fromEntity(entity));
        }
        return ResultResp.ok(resp);
    }

}
